/////////////////////////////////////////////////////
// ruler.sqf
// by nou
//
// please see the documentation in add_ruler.sqf
//
// There is a lot of things that should be documented in here
// but I do not have the time right now. It is pretty easy to
// follow though. I hope! ;)
/////////////////////////////////////////////////////

disableSerialization;
_unit 	= _this select 0;
_params = _this select 1;

_ctrlId 	= _params select 0;
_mouseBtnId	= _params select 1;
_scrX		= _params select 2;
_scrY		= _params select 3;
_shiftBtn	= _params select 4;
_ctrlBtn	= _params select 5;
_altBtn		= _params select 6;

_colors = ["ColorBlack", "ColorBlue", "ColorBrown", "ColorGreen", "ColorRed", "ColorKhaki", "ColorPink", "ColorYellow", "ColorWhite"];


_keyDown = _unit getVariable "nou_ruler_keyDown";

_pos			= _ctrlId ctrlMapScreenToWorld [_scrX, _scrY];
_rulerStatus 	= _unit getVariable "Nou_Map_Ruler_Status";
_startPos 		= _unit getVariable "Nou_Map_Ruler_StartPos";
_endPos			= _unit getVariable "Nou_Map_Ruler_EndPos";

_rulerLength = 1550;
_divisor = 25;
_slices = floor ((_rulerLength*2)/_divisor);

_degToMil 		= 17.777777777778;
_degToMilSoviet = 16.6666667;
switch(_keyDown) do {
	case 35: {
		_startPos = _pos;
		
		deleteMarkerLocal "ruler_origin";
		deleteMarkerLocal "ruler_baseLine";
		for [{_i=0;},{_i < _slices+1},{_i=_i+1}] do {
			_rectName = format["slice_rect_%1", _i];
			deleteMarkerLocal _rectName;
			_markerName = format["slice_marker_%1", _i];
			deleteMarkerLocal _markerName;
		};
		
		_unit setVariable ["Nou_Map_Ruler_StartPos", _startPos];
		
		_originMarkerName = "ruler_origin";
		_origin = createMarkerLocal [_originMarkerName, _startPos];
		_origin setMarkerShapeLocal "ICON";
		_origin setMarkerTypeLocal "DOT";
		_origin setMarkerTextLocal "";
		_origin setMarkerColorLocal "ColorRed";
		
		
	};
	case 36: {
		deleteMarkerLocal "ruler_baseLine";
		for [{_i=0;},{_i < _slices+1},{_i=_i+1}] do {
			_rectName = format["slice_rect_%1", _i];
			deleteMarkerLocal _rectName;
			_markerName = format["slice_marker_%1", _i];
			deleteMarkerLocal _markerName;
		};
		_endPos = _pos;
		_x1 = _startPos select 0;
		_x2 = _endPos select 0;
		_y1 = _startPos select 1;
		_y2 = _endPos select 1;
		_az = (180 + (((_x1 - _x2) atan2 (_y1 - _y2))) mod 360);
		_baseLine = createMarkerLocal ["ruler_baseLine", _startPos];
		_baseLine setMarkerShapeLocal "RECTANGLE";
		_baseLine setMarkerSizeLocal [2, _rulerLength];
		_baseLine setMarkerColorLocal "ColorRed";
		_baseLine setMarkerDirLocal _az;
		
		
		
		
		_sliceStartX = (_startPos select 0) - sin(_az)*_rulerLength;
		_sliceStartY = (_startPos select 1) - cos(_az)*_rulerLength;
		_sliceStart = _startPos;
		_dist = (_rulerLength*-1)-25;
		for [{_i=0;},{_i < _slices+1},{_i=_i+1}] do {
			_dist = _dist + _divisor;
			_posX = (_sliceStart select 0) + sin(_az)*_dist;
			_posY = (_sliceStart select 1) + cos(_az)*_dist;
			_text = "";
			_width = 2;
			_color = "ColorBlue";
			_height = 25;
			
			_markerAz = (_az + 90) mod 180;
			_markerPosX = (_posX) + sin(_markerAz)*30;
			_markerPosY = (_posY) + cos(_markerAz)*30;
			
			if(_dist%25 == 0) then {
				_width = 1;
				_height = 6.25;
				_color = "ColorBlack";
			};
			if(_dist%50 == 0) then {
				_width = 1.5;
				_height = 12.5;
				_color = "ColorGreen";
			};
			if(_dist%100 == 0) then {
				_width = 2;
				_text = format["%1m", _dist];
				_height = 25;
				_color = "ColorBlue";
			};
			if(_dist == 0) then {
				_width = 2;
				_text = format["%1m | Deg: %2 | Mil64: %3 | Mil60: %4", _dist, (round _az), ( (round ((_az*_degToMil)/10))*10), ( (round ((_az*_degToMilSoviet)/10))*10)];
				_height = 50;
				_color = "ColorRed";
				_markerPosX = (_posX) + sin(_markerAz)*55;
				_markerPosY = (_posY) + cos(_markerAz)*55;
			};
			
			
			
			
			_markerName = format["slice_marker_%1", _i];
			_marker = createMarkerLocal [_markerName, [(_markerPosX), (_markerPosY)]];
			_marker setMarkerShapeLocal "ICON";
			_marker setMarkerTypeLocal "DOT";
			_marker setMarkerSizeLocal [0, 0];
			_marker setMarkerTextLocal _text;
			_marker setMarkerColorLocal "ColorRedFaded25";
			
			
			_rectName = format["slice_rect_%1", _i];
			_rect = createMarkerLocal [_rectName, [_posX, _posY]];
			_rect setMarkerShapeLocal "RECTANGLE";
			_rect setMarkerSizeLocal [_height, _width];
			_rect setMarkerColorLocal _color;
			_rect setMarkerDirLocal _az;
		};
		_unit setVariable ["Nou_Map_Ruler_EndPos", _endPos];
	};
	case 37: {
		_pos	= _ctrlId ctrlMapScreenToWorld [_scrX, _scrY];
		deleteMarkerLocal "compass_origin";
		_originMarkerName = "compass_origin";
		_origin = createMarkerLocal [_originMarkerName, _pos];
		_origin setMarkerShapeLocal "ICON";
		_origin setMarkerTypeLocal "DOT";
		_origin setMarkerTextLocal "";
		_origin setMarkerSizeLocal [.5, .5];
		_origin setMarkerColorLocal "ColorRed";
		
		
		_increment = 1;
		_sliceId = 0;
        
		for [{_az=0;},{_az < 360},{_az=_az+_increment}] do {
			_rectName = format["nou_compass_marker_%1", _sliceId];
			deleteMarkerLocal _rectName;
			_markerName = format["nou_compass_az_%1", _sliceId];
			deleteMarkerLocal _markerName;
			_sliceId = _sliceId + 1;
		};
		_sliceId = 0;
		for [{_az=0;},{_az < 360},{_az=_az+_increment}] do {
			
			_posX = (_pos select 0) + sin(_az)*157.5;
			_posY = (_pos select 1) + cos(_az)*157.5;
			_size = 2.5;
			_color = "ColorBlack";
			_width = 0.50;
			if(_az%5 == 0) then {
				_color = "ColorBlue";
				_size = 10;
				_width = 1;
                _posX = (_pos select 0) + sin(_az)*150;
                _posY = (_pos select 1) + cos(_az)*150;
			};		
			
			if(_az%10 == 0) then {
				_color = "ColorGreen";
				_size = 15;
				_width = 1;
                _posX = (_pos select 0) + sin(_az)*145;
                _posY = (_pos select 1) + cos(_az)*145;
			};		
			if(_az%20 == 0) then {
				_color = "ColorRed";
				_size = 20;
				_width = 1;
                _posX = (_pos select 0) + sin(_az)*140;
                _posY = (_pos select 1) + cos(_az)*140;
			};
			
			
			_rectName = format["nou_compass_marker_%1", _sliceId];
			_rect = createMarkerLocal [_rectName, [_posX, _posY]];
			_rect setMarkerShapeLocal "RECTANGLE";
			_rect setMarkerSizeLocal [_width, _size];
			_rect setMarkerColorLocal _color;
			_rect setMarkerDirLocal _az;
			
			if(_az%20 == 0) then {
				_markerAz = (_az + 90) mod 180;
				_markerPosX = (_pos select 0) + sin(_az)*120;
				_markerPosY = (_pos select 1) + cos(_az)*120;
				_markerName = format["nou_compass_az_%1", _sliceId];
				_marker = createMarkerLocal [_markerName, [(_markerPosX), (_markerPosY)]];
				_marker setMarkerShapeLocal "ICON";
				_marker setMarkerTypeLocal "DOT";
				_marker setMarkerSizeLocal [0.25, 0.25];
				_marker setMarkerTextLocal format["%1", _az];
				_marker setMarkerColorLocal "ColorRedFaded25";
			};
			
			_sliceId = _sliceId + 1;
		};
        
        //mils
        _sliceId = 0;
        for [{_az=0;},{_az < 320},{_az=_az+_increment}] do {
			_rectName = format["nou_compass_marker_mil_%1", _sliceId];
			deleteMarkerLocal _rectName;
			_markerName = format["nou_compass_az_mil_%1", _sliceId];
			deleteMarkerLocal _markerName;
			_sliceId = _sliceId + 1;
		};
		_sliceId = 0;
		for [{_mil=0;},{_mil < 320},{_mil=_mil+_increment}] do {
			_az = _mil*1.12500;
			_posX = (_pos select 0) + sin(_az)*253;
			_posY = (_pos select 1) + cos(_az)*253;
			_size = 7;
			_color = "ColorBlack";
			_width = 0.75;
			if(_mil%5 == 0) then {
				_color = "ColorBlue";
				_size = 10;
				_width = 1;
                _posX = (_pos select 0) + sin(_az)*250;
                _posY = (_pos select 1) + cos(_az)*250;
			};		
			
			if(_mil%10 == 0) then {
				_color = "ColorGreen";
				_size = 25;
				_width = 1;
                _posX = (_pos select 0) + sin(_az)*235;
                _posY = (_pos select 1) + cos(_az)*235;
			};
			
			
			_rectName = format["nou_compass_marker_mil_%1", _sliceId];
			_rect = createMarkerLocal [_rectName, [_posX, _posY]];
			_rect setMarkerShapeLocal "RECTANGLE";
			_rect setMarkerSizeLocal [_width, _size];
			_rect setMarkerColorLocal _color;
			_rect setMarkerDirLocal _az;
			
			if(_mil%10 == 0) then {
				_markerAz = (_az + 90) mod 180;
				_markerPosX = (_pos select 0) + sin(_az)*210;
				_markerPosY = (_pos select 1) + cos(_az)*210;
                //_markerPosY = _markerPosY + 15;
				_markerName = format["nou_compass_az_mil_%1", _sliceId];
				_marker = createMarkerLocal [_markerName, [(_markerPosX), (_markerPosY)]];
				_marker setMarkerShapeLocal "ICON";
				_marker setMarkerTypeLocal "DOT";
				_marker setMarkerSizeLocal [0.25, 0.25];
                if(_mil == 0) then {
                    _marker setMarkerTextLocal format["%1", 64];
                } else {
                    _marker setMarkerTextLocal format["%1", (_mil/20)*4];
                };
				_marker setMarkerColorLocal "ColorRedFaded25";
			};
			
			_sliceId = _sliceId + 1;
		};
	};

	case 38: {
		deleteMarkerLocal "compass_origin";
		
		_increment = 1;
		_sliceId = 0;
		
		for [{_az=0;},{_az < 360},{_az=_az+_increment}] do {
			_rectName = format["nou_compass_marker_%1", _sliceId];
			deleteMarkerLocal _rectName;
			_markerName = format["nou_compass_az_%1", _sliceId];
			deleteMarkerLocal _markerName;
			_sliceId = _sliceId + 1;
		};
        _sliceId = 0;
        for [{_az=0;},{_az < 320},{_az=_az+_increment}] do {
			_rectName = format["nou_compass_marker_mil_%1", _sliceId];
			deleteMarkerLocal _rectName;
			_markerName = format["nou_compass_az_mil_%1", _sliceId];
			deleteMarkerLocal _markerName;
			_sliceId = _sliceId + 1;
		};
	};
    
    case 26: {
        _oldPos = _unit getVariable "Nou_Map_Line_Segment_Start";
        _colorNumber = _unit getVariable "Nou_Map_Line_Color";
        if((_oldPos select 0) == (_pos select 0) && (_oldPos select 1) == (_pos select 1)) then {
            _colorNumber = _colorNumber + 1;
            if(_colorNumber > (count _colors)-1) then {
                _colorNumber = 0;
            };
            
        };
        _unit setVariable ["Nou_Map_Line_Color", _colorNumber];
        _color = _colors select _colorNumber;
        _unit setVariable ["Nou_Map_Line_Segment_Start", _pos];
        _originMarkerName = "line_origin";
        deleteMarkerLocal _originMarkerName;
		_origin = createMarkerLocal [_originMarkerName, _pos];
		_origin setMarkerShapeLocal "ICON";
		_origin setMarkerTypeLocal "DOT";
		_origin setMarkerTextLocal "";
		_origin setMarkerColorLocal _color;
    };
    
    case 27: {
        _ctrl = ((findDisplay 12) displayCtrl 51);
        _startPos = _unit getVariable "Nou_Map_Line_Segment_Start";
        _endPos = _pos;
        
        _x1 = _startPos select 0;
		_x2 = _endPos select 0;
		_y1 = _startPos select 1;
		_y2 = _endPos select 1;
		_az = (180 + (((_x1 - _x2) atan2 (_y1 - _y2))) mod 360);
        _distance = [_x1, _y1, 0] distance [_x2, _y2, 0];
        _posX = (_startPos select 0) + sin(_az)*(_distance/2);
		_posY = (_startPos select 1) + cos(_az)*(_distance/2);
        
        _count = _unit getVariable "Nou_Map_Line_Number";
        _count = _count + 1;
        _unit setVariable ["Nou_Map_Line_Number", _count];
        
        _originMarkerName = "line_origin";
        deleteMarkerLocal _originMarkerName;
        
        _name = format["line_segment_%1", _count];
        _colorNumber = _unit getVariable "Nou_Map_Line_Color";
        _color = _colors select _colorNumber;
        
        _line = createMarkerLocal [_name, [_posX, _posY, 0]];
		_line setMarkerShapeLocal "RECTANGLE";
		_line setMarkerSizeLocal [4, (_distance/2)];
		_line setMarkerColorLocal _color;
		_line setMarkerDirLocal _az;
    };
    
    case 43: {
        _count = _unit getVariable "Nou_Map_Line_Number";
        if(_count >= 0) then {
            _name = format["line_segment_%1", _count];
            deleteMarkerLocal _name;
            _count = _count - 1;
            _unit setVariable ["Nou_Map_Line_Number", _count];
        };
    };
};